package com.qf.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.qf.model.entity.Testpackage;
import com.qf.service.TestpackageService;

import java.util.ArrayList;
import java.util.List;

public class TestpackageListener extends AnalysisEventListener<Testpackage> {


    // 用于存放临时的SysUser列表
    private List<Testpackage> testpackageList = new ArrayList<>();
    // 配置每次批量导入5条
    private static  final  int BATCH_COUNT=5;

    private TestpackageService testpackageService; // 构建时传递studentService

    public TestpackageListener() {
    }

    public TestpackageListener(TestpackageService testpackageService) {
        this.testpackageService = testpackageService;
    }


    @Override
    public void invoke(Testpackage testpackage, AnalysisContext analysisContext) {
        testpackageList.add(testpackage);

        if (testpackageList.size()>= BATCH_COUNT){
            // 批量导入
            testpackageService.insertBatch(testpackageList);
            // 清空
            testpackageList.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        // 最后也要批量导入
        if (testpackageList.size()>0){
            testpackageService.insertBatch(testpackageList);
            testpackageList.clear();
        }
    }
}
